layui.use(['layer', 'form', 'table', 'admin',  'laydate','ax',"upload","laypage"], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var laypage = layui.laypage;
    /**
     * 系统管理--评价管理
     */
    var OBJ = {
        tableId: "meterReadingTable",    //表格id
        condition: {
            meterNo: "",
            shopName:""
        }
    };

    /**
     * 初始化表格的列
     */
    OBJ.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'meterRedingMonth', sort: true, title: '所属月份'},
            {field: 'shopAdr', sort: true, title: '铺位号'},
            {field: 'shopName', sort: true, title: '品牌名称'},
            {field: 'contractCode', sort: true, title: '合同编号'},
            {field: 'contractStartDate', sort: true, title: '合同开始日期'},
            {field: 'contractEndDate', sort: true, title: '合同结束日期'},
            {field: 'meterName', sort: true, title: '能源表类型'},
            {field: 'meterNo', sort: true, title: '能源表编号'},
            {field: 'meterModelName', sort: true, title: '能源表模式'},
            {field: 'startMum', sort: true, title: '起码'},
            {field: 'endMum', sort: true, title: '止码'}
        ]];
    };
    /**
     * 点击查询按钮
     */
    OBJ.search = function () {
        var queryData = {};
        queryData['monthTime'] = $("#monthTime").val();
        queryData['meterModel'] = $("#meterModel").val();
        table.reload(OBJ.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };
    //渲染时间选择框
    //年月选择器
    laydate.render({
        elem: '#monthTime'
        ,type: 'month'
    });

    /**
     * 导出excel按钮
     */
    OBJ.exportExcel = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        var monthTime= $("#monthTime").val();
        if(monthTime==''){
            Feng.error("请进行月份筛选！");
            return;
        }
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data,'xls');

        }
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OBJ.tableId,
        url: Feng.ctxPath + '/meterTemplate/list',
        page: {
            limits:[10,20,30,50,100,'ALL']
        },
        height: "full-158",
        title:"抄表模板",
        cellMinWidth: 100,
        cols: OBJ.initColumn(),
        done:function(res, curr, count){
            if (this.limit ==count){
                $(".layui-laypage-limits select").find("option[value='ALL']").attr("selected",true);
            }
            $(".layui-laypage-limits select").find("option[value='ALL']").attr("value",count).text('全部');
        }

    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OBJ.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OBJ.exportExcel();
    });
    //重置
    $("#reset").click(function () {
        var queryData = {};
        $("#monthTime").val("");
        $("#meterModel").val("");
        form.render();
        queryData['monthTime'] = $("#monthTime").val();
        queryData['meterModel'] = $("#meterModel").val();
        table.reload(OBJ.tableId,{
            where: queryData
        })
        table.reload(OBJ.tableId);

    })


    // 工具条点击事件
    table.on('tool(' + OBJ.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dispatch') {
            OBJ.onDispatchTicket(data);
        } else if (layEvent === 'delete') {
            OBJ.onDeleteTicket(data);
        } else if (layEvent === 'detail') {
            OBJ.onDetailPayment(data);
        }
    });
    //导出全部
    $('#exportAll').on('click', function(){
        // 模拟从后端接口读取需要导出的数据
        var checkRows = table.checkStatus(OBJ.tableId);
        var monthTime= $("#monthTime").val();
        if(monthTime==''){
            Feng.error("请进行月份筛选！");
            return;
        }
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            var ids = "";
            for (var i = 0; i < checkRows.data.length; i++) {
                ids += checkRows.data[i].id + ",";
            }
            window.location.href=Feng.ctxPath + '/meterTemplate/export?monthTime='+monthTime+'&ids='+ids;
        }


    });
});
